Car fleet¶
Time: O(NLogN); Space: O(N); medium
N cars are going to the same destination along a one lane road. The destination is target miles away.
Each car i has a constant speed speed[i] (in miles per hour), and initial position position[i] miles towards the target along the road.
A car can never pass another car ahead of it, but it can catch up to it, and drive bumper to bumper at the same speed.
The distance between these two cars is ignored - they are assumed to have the same position.
A car fleet is some non-empty set of cars driving at the same position and same speed. Note that a single car is also a car fleet.
If a car catches up to a car fleet right at the destination point, it will still be considered as one car fleet.
How many car fleets will arrive at the destination?
Example 1:
Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
Output: 3
Explanation:
The cars starting at 10 and 8 become a fleet, meeting each other at 12.
The car starting at 0 doesn’t catch up to any other car, so it is a fleet by itself.
The cars starting at 5 and 3 become a fleet, meeting each other at 6.
Note that no other cars meet these fleets before the destination, so the answer is 3.
Example 2:
Input: target = 20, position = [6,2,17], speed = [3,9,2]
Output: 2
Explanation:
The cars starting at 6 and 2 become a fleet, meeting each other at 18.
The other cars from the 17th car can’t catch up with it, so it’s a team.
Note that no other cars meet these fleets before the destination, so the answer is 2.
Constraints:
0 <= N <= 10 ^ 4
0 < target <= 10 ^ 6
0 < speed[i] <= 10 ^ 6
0 <= position[i] < target
All initial positions are different.
1. Sort¶
Intuition
Call the “lead fleet” the fleet furthest in position.
If the car S (Second) behind the lead car F (First) would arrive earlier, then S forms a fleet with the lead car F. Otherwise, fleet F is final as no car can catch up to it - cars behind S would form fleets with S, never F.
Algorithm
A car is a (position, speed) which implies some arrival time (target - position) / speed. Sort the cars by position.
Now apply the above reasoning - if the lead fleet drives away, then count it and continue. Otherwise, merge the fleets and continue.
[6]:
class Solution1(object):
"""
Time: O(NLogN), where N is the number of cars. The complexity is dominated by the sorting operation.
Space: O(N), the space used to store information about the cars.
"""
def carFleet(self, target, position, speed):
"""
:type target: int
:type position: List[int]
:type speed: List[int]
:rtype: int
"""
cars = sorted(zip(position, speed))
times = [float(target - p) / s for p, s in cars]
result = 0
while len(times) > 1:
lead = times.pop()
if lead < times[-1]:
result += 1 # if lead arrives sooner, it can't be caught
else:
times[-1] = lead # else, fleet arrives at later time 'lead'
return result + bool(times) # remaining car is fleet (if it exists)
[7]:
s = Solution1()
target = 12
position = [10,8,0,5,3]
speed = [2,4,1,1,3]
assert s.carFleet(target, position, speed) == 3
target = 20
position = [6,2,17]
speed = [3,9,2]
assert s.carFleet(target, position, speed) == 2
[8]:
class Solution2(object):
def carFleet(self, target, position, speed):
"""
:type target: int
:type position: List[int]
:type speed: List[int]
:rtype: int
"""
times = [float(target-p)/s for p, s in sorted(zip(position, speed))]
result, curr = 0, 0
for t in reversed(times):
if t > curr:
result += 1
curr = t
return result
[9]:
s = Solution2()
target = 12
position = [10,8,0,5,3]
speed = [2,4,1,1,3]
assert s.carFleet(target, position, speed) == 3
target = 20
position = [6,2,17]
speed = [3,9,2]
assert s.carFleet(target, position, speed) == 2